package com.fqdf.framework.android.activity;


import android.app.Activity;
import android.app.Application;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentActivity;

import com.afollestad.materialdialogs.MaterialDialog;
import com.fqdf.framework.android.util.DialogUtil;

public class MyBaseActivity extends FragmentActivity {

    private Application mApplication;
    private MaterialDialog submitNoCancelprogressDialog;
    private MaterialDialog progressDialog;
    private MaterialDialog noCancelprogressDialog;
    private MaterialDialog createNoCancelUploadImgDialog;

    private AppLifecycleStatus appLifecycleStatus;

    public enum AppLifecycleStatus{
        OnCreate,
        OnStart,
        OnResume,
        OnPause,
        OnStop,
        OnRestart,
        OnDestory
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        appLifecycleStatus = AppLifecycleStatus.OnCreate;
        super.onCreate(savedInstanceState);
    }

    /**
     * 当前生命周期的状态是否是oncreate状态
     * @return
     */
    public boolean lifeCycleIsCreate(){
        return appLifecycleStatus==AppLifecycleStatus.OnCreate;
    }

    @Override
    protected void onStart() {
        appLifecycleStatus = AppLifecycleStatus.OnStart;
        super.onStart();
    }

    /**
     * 当前生命周期的状态是否是OnStart状态
     * @return
     */
    public boolean lifeCycleIsStart(){
        return appLifecycleStatus==AppLifecycleStatus.OnStart;
    }

    @Override
    protected void onResume() {
        appLifecycleStatus = AppLifecycleStatus.OnResume;
        super.onResume();
    }

    /**
     * 当前生命周期的状态是否是OnResume状态
     * @return
     */
    public boolean lifeCycleIsResume(){
        return appLifecycleStatus==AppLifecycleStatus.OnResume;
    }

    @Override
    protected void onPause() {
        appLifecycleStatus = AppLifecycleStatus.OnPause;
        super.onPause();
    }

    /**
     * 当前生命周期的状态是否是onPause状态
     * @return
     */
    public boolean lifeCycleIsPause(){
        return appLifecycleStatus==AppLifecycleStatus.OnPause;
    }

    @Override
    protected void onRestart() {
        appLifecycleStatus = AppLifecycleStatus.OnRestart;
        super.onRestart();
    }

    /**
     * 当前生命周期的状态是否是onRestart状态
     * @return
     */
    public boolean lifeCycleIsRestart(){
        return appLifecycleStatus==AppLifecycleStatus.OnRestart;
    }

    @Override
    protected void onStop() {
        appLifecycleStatus = AppLifecycleStatus.OnStop;
        super.onStop();
        hidenMaterialDialog(submitNoCancelprogressDialog);
        hidenMaterialDialog(progressDialog);
        hidenMaterialDialog(noCancelprogressDialog);
        hidenMaterialDialog(createNoCancelUploadImgDialog);
    }

    /**
     * 当前生命周期的状态是否是onStop状态
     * @return
     */
    public boolean lifeCycleIsStop(){
        return appLifecycleStatus==AppLifecycleStatus.OnStop;
    }

    @Override
    protected void onDestroy() {
        appLifecycleStatus = AppLifecycleStatus.OnDestory;
        super.onDestroy();
    }

    /**
     * 当前生命周期的状态是否是onDestroy状态
     * @return
     */
    public boolean lifeCycleIsDestroy(){
        return appLifecycleStatus==AppLifecycleStatus.OnDestory;
    }

    public  <T extends Application> T getMyApplication() {
        if (mApplication == null) {
            mApplication = getApplication();
        }
        return (T) mApplication;
    }

    /**
     * 获取提交等待框-不可取消
     *
     * @return
     */
    protected MaterialDialog getNoCancelSubmitDialog() {
        if (submitNoCancelprogressDialog == null) {
            submitNoCancelprogressDialog = DialogUtil.createNoCancelSubmit(this);
        }
        return submitNoCancelprogressDialog;
    }

    /**
     * 获取加载等待框
     *
     * @return
     */
    protected MaterialDialog getProgressDialog() {
        if (progressDialog == null) {
            progressDialog = DialogUtil.createLoadingDialog(this);
        }
        return progressDialog;
    }

    /**
     * 获取加载等待框--不可取消
     *
     * @return
     */
    protected MaterialDialog getNoCancelProgressDialog() {
        if (noCancelprogressDialog == null) {
            noCancelprogressDialog = DialogUtil.createNoCancelLoadingDialog(this);
        }
        return noCancelprogressDialog;
    }

    /**
     * 创建上传文件等待的dialog
     *
     * @return
     */
    protected MaterialDialog getUploadImgProgressDialog() {
        if (createNoCancelUploadImgDialog == null) {
            createNoCancelUploadImgDialog = DialogUtil.createNoCancelUploadImgDialog(this);
        }
        return createNoCancelUploadImgDialog;
    }

    /**
     * 隐藏dialog
     *
     * @param dialog
     */
    protected void hidenMaterialDialog(MaterialDialog dialog) {
        DialogUtil.hideIndeterminateProgress(dialog);

    }

}
